  app.service('ordersService',['$http','$q','utilService','$cookies',function($http,$q,utilService,$cookies){

    this.findById = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'orders/goods/view',
            params:{orderId:$scope.id}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.updateStatus = function($scope){
        console.info($scope.app.domain);
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'orders/update',
            data:$scope.order,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.getList = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'logistics/list',
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.getOrderById = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'orders/info',
            params:{orderId:$scope.id}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.updateLogistics = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'orders/update/logistics',
            data:{orderId:$scope.orders.id,logisticsId:$scope.orders.logisticsId,logisticsNumber:$scope.orders.logisticsNumber},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.logisticsTrace=function($scope){
        var deferred=$q.defer();
        utilService.get({
           url:$scope.app.domain +'logistics/trace',
            params:$scope.logistics
        }).then(function(result){
            deferred.resolve(result.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    }

    this.updateOrdersPrice=function($scope){
        var orderPriceVo={};
        orderPriceVo.orderId=$scope.orders.id;
        orderPriceVo.payMoney=$scope.orders.changedPayMoney;
        orderPriceVo.priceDifference=$scope.orders.changedPayMoney-$scope.orders.payMoney;
        orderPriceVo.userPhone=$scope.orders.userPhone;
        orderPriceVo.phoneCode=$scope.orders.phoneCode;
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'orders/update/price',
            data:orderPriceVo,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //发送手机验证码
    this.getMobilePhoneCode = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'orders/getMobilePhoneCode',
            params:{mobilePhone:$scope.orders.userPhone}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    //验证手机白名单
    this.checkPhone = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'system/user/check/white/list/phone',
            params:{phone:$scope.orders.userPhone}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    //核销
    this.checkCode = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'orders/update',
            data:$scope.ordersCode,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.changeGoodsStatus = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'orders/goods/status',
            data:$scope.ordersRefundVO,
            method:'PUT'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    };

    this.checkShop = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'shop/session',
            params:{sessionId:$cookies.sessionId}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    };

    this.findMinMoney = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'orders/coupons',
            params:{orderId:$scope.id}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    };

    this.findPrice = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'orders/goods/price',
            params:{ordersGoodsId:$scope.ordersRefundVO.ordersGoodsId}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    };

    //导出数据
    this.exportData = function($scope){
        $scope.ordersdetails.sessionId=$cookies.sessionId;
        $http({
            method:"POST",
            url:$scope.app.domain + 'report/orders/details',
            params:$scope.ordersdetails,
            responseType: "arraybuffer",
            headers:{'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                }
                return str.join("&");
            }
        }).success(function(data){
            var blob = new Blob([data], {type: "application/vnd.ms-excel"});
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.download = $scope.ordersdetails.ordersExportName;
            a.href = URL.createObjectURL(blob);
            a.click();
        })
    }

    //查询预约订单信息
    this.checkReserve = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'orders/reserve',
            params:{orderId:$scope.id}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }
}]);